<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: udpClient.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>udpClient.c</h1><a href="udp_client_8c.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* fpont 12/99 */</span>
<a name="l00002"></a>00002 <span class="comment">/* pont.net    */</span>
<a name="l00003"></a>00003 <span class="comment">/* udpClient.c */</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;sys/socket.h&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;netinet/in.h&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;netdb.h&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;string.h&gt;</span> <span class="comment">/* memset() */</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;sys/time.h&gt;</span> <span class="comment">/* select() */</span> 
<a name="l00014"></a>00014 
<a name="l00015"></a><a class="code" href="udp_client_8c.html#755a7607fae53ff74132fd57e5b652ad">00015</a> <span class="preprocessor">#define REMOTE_SERVER_PORT 1500</span>
<a name="l00016"></a><a class="code" href="udp_client_8c.html#a24597a54a085c6c2c33b64138f09eff">00016</a> <span class="preprocessor"></span><span class="preprocessor">#define MAX_MSG 100</span>
<a name="l00017"></a>00017 <span class="preprocessor"></span>
<a name="l00018"></a>00018 
<a name="l00019"></a><a class="code" href="udp_client_8c.html#0ddf1224851353fc92bfbff6f499fa97">00019</a> <span class="keywordtype">int</span> <a class="code" href="test__hashtable_8c.html#3c04138a5bfe5d72780bb7e82a18e627">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {
<a name="l00020"></a>00020   
<a name="l00021"></a>00021   <span class="keywordtype">int</span> sd, rc, i;
<a name="l00022"></a>00022   <span class="keyword">struct </span>sockaddr_in cliAddr, remoteServAddr;
<a name="l00023"></a>00023   <span class="keyword">struct </span>hostent *h;
<a name="l00024"></a>00024   <span class="keywordtype">int</span> <a class="code" href="heartbeat_8h.html#63e6914e51b6e79ae83f8c548c7a9e70" title="Envia a mensagem em Broadcast?">broadcast</a> = 1;
<a name="l00025"></a>00025 
<a name="l00026"></a>00026   <span class="comment">/* check command line args */</span>
<a name="l00027"></a>00027   <span class="keywordflow">if</span>(argc&lt;3) {
<a name="l00028"></a>00028     printf(<span class="stringliteral">"usage : %s &lt;server&gt; &lt;data1&gt; ... &lt;dataN&gt; \n"</span>, argv[0]);
<a name="l00029"></a>00029     exit(1);
<a name="l00030"></a>00030   }
<a name="l00031"></a>00031 
<a name="l00032"></a>00032   <span class="comment">/* get server IP address (no check if input is IP address or DNS name */</span>
<a name="l00033"></a>00033   h = gethostbyname(argv[1]);
<a name="l00034"></a>00034   <span class="keywordflow">if</span>(h==NULL) {
<a name="l00035"></a>00035     printf(<span class="stringliteral">"%s: unknown host '%s' \n"</span>, argv[0], argv[1]);
<a name="l00036"></a>00036     exit(1);
<a name="l00037"></a>00037   }
<a name="l00038"></a>00038 
<a name="l00039"></a>00039   printf(<span class="stringliteral">"%s: sending data to '%s' (IP : %s) \n"</span>, argv[0], h-&gt;h_name,
<a name="l00040"></a>00040          inet_ntoa(*(<span class="keyword">struct</span> in_addr *)h-&gt;h_addr_list[0]));
<a name="l00041"></a>00041 
<a name="l00042"></a>00042   remoteServAddr.sin_family = h-&gt;h_addrtype;
<a name="l00043"></a>00043   memcpy((<span class="keywordtype">char</span> *) &amp;remoteServAddr.sin_addr.s_addr, 
<a name="l00044"></a>00044          h-&gt;h_addr_list[0], h-&gt;h_length);
<a name="l00045"></a>00045   remoteServAddr.sin_port = htons(<a class="code" href="udp_client_8c.html#755a7607fae53ff74132fd57e5b652ad">REMOTE_SERVER_PORT</a>);
<a name="l00046"></a>00046 
<a name="l00047"></a>00047   <span class="comment">/* socket creation */</span>
<a name="l00048"></a>00048   sd = socket(AF_INET,SOCK_DGRAM,0);
<a name="l00049"></a>00049   <span class="keywordflow">if</span>(sd&lt;0) {
<a name="l00050"></a>00050     printf(<span class="stringliteral">"%s: cannot open socket \n"</span>,argv[0]);
<a name="l00051"></a>00051     exit(1);
<a name="l00052"></a>00052   }
<a name="l00053"></a>00053   
<a name="l00054"></a>00054   <span class="keywordflow">if</span>((setsockopt(sd,SOL_SOCKET,SO_BROADCAST,
<a name="l00055"></a>00055                            &amp;broadcast,<span class="keyword">sizeof</span> broadcast)) == -1)
<a name="l00056"></a>00056    {
<a name="l00057"></a>00057            perror(<span class="stringliteral">"setsockopt - SO_SOCKET "</span>);
<a name="l00058"></a>00058            exit(1);
<a name="l00059"></a>00059    }
<a name="l00060"></a>00060 
<a name="l00061"></a>00061   
<a name="l00062"></a>00062   <span class="comment">/* bind any port */</span>
<a name="l00063"></a>00063   cliAddr.sin_family = AF_INET;
<a name="l00064"></a>00064   cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
<a name="l00065"></a>00065   cliAddr.sin_port = htons(0);
<a name="l00066"></a>00066   
<a name="l00067"></a>00067   rc = bind(sd, (<span class="keyword">struct</span> sockaddr *) &amp;cliAddr, <span class="keyword">sizeof</span>(cliAddr));
<a name="l00068"></a>00068   <span class="keywordflow">if</span>(rc&lt;0) {
<a name="l00069"></a>00069     printf(<span class="stringliteral">"%s: cannot bind port\n"</span>, argv[0]);
<a name="l00070"></a>00070     exit(1);
<a name="l00071"></a>00071   }
<a name="l00072"></a>00072 
<a name="l00073"></a>00073 
<a name="l00074"></a>00074   <span class="comment">/* send data */</span>
<a name="l00075"></a>00075   <span class="keywordflow">for</span>(i=2;i&lt;argc;i++) {
<a name="l00076"></a>00076     rc = sendto(sd, argv[i], strlen(argv[i])+1, 0, 
<a name="l00077"></a>00077                 (<span class="keyword">struct</span> sockaddr *) &amp;remoteServAddr, 
<a name="l00078"></a>00078                 <span class="keyword">sizeof</span>(remoteServAddr));
<a name="l00079"></a>00079 
<a name="l00080"></a>00080     <span class="keywordflow">if</span>(rc&lt;0) {
<a name="l00081"></a>00081       printf(<span class="stringliteral">"%s: cannot send data %d \n"</span>,argv[0],i-1);
<a name="l00082"></a>00082       close(sd);
<a name="l00083"></a>00083       exit(1);
<a name="l00084"></a>00084     }
<a name="l00085"></a>00085 
<a name="l00086"></a>00086   }
<a name="l00087"></a>00087   
<a name="l00088"></a>00088   <span class="keywordflow">return</span> 1;
<a name="l00089"></a>00089 
<a name="l00090"></a>00090 }
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 22:06:26 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
